﻿using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Event;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.UI.Data;
using CPPEI.Coolzon.Event.Core;
using Coolvis.Console.Theme.MainViewControl;
using Coolvis.UI.Theme.Console;

namespace Coolvis.Console.Theme
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : CPPEI.Coolzon.UI.View, IMainView
    {
        SelectSceneView _viewSelectScene;

        private Scene _scene;
        public MainView()
        {
            InitializeComponent();
            ExtentionFunctionLoader.Instance.Load(string.Format("{0}/config/extentionfunction.xml", AppDomain.CurrentDomain.BaseDirectory)); 
        }

        protected override void OnActive()
        {
            base.OnActive();

            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            sceneContent.Child = renderView;


            _viewSelectScene = new SelectSceneView();
            CPPEI.Coolzon.Common.EventBus.Instance.Register(_viewSelectScene);
            CoreEventBus.Register(_viewSelectScene);

            // 将事件远程处理模式的代理实例注册到事件总线上
            RemoteEventProxy.Instance.SetRenderView(renderView);

            CPPEI.Coolzon.Common.EventBus.Instance.Register(header);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(sceneContent);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(operationNavigation);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(jsControl);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(zoomView);
            CPPEI.Coolzon.Common.EventBus.Instance.Register(this);

            NCLEventBus.Register(header);
            NCLEventBus.Register(sceneContent);
            NCLEventBus.Register(operationNavigation);
            NCLEventBus.Register(jsControl);
            NCLEventBus.Register(zoomView);
            NCLEventBus.Register(this);

            UIEventBus.Register(header);
            UIEventBus.Register(sceneContent);
            UIEventBus.Register(operationNavigation);
            UIEventBus.Register(jsControl);
            UIEventBus.Register(zoomView);
            UIEventBus.Register(this);

            CoreEventBus.Register(header);
            CoreEventBus.Register(sceneContent);
            CoreEventBus.Register(operationNavigation);
            CoreEventBus.Register(jsControl);
            CoreEventBus.Register(zoomView);
            CoreEventBus.Register(this);

            CPPEI.Coolzon.Common.EventBus.Create(CPPEI.Coolzon.ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Register(renderView);

            OpenDefaultSceneThread.Start(UIContext.Current.Solution);
        }

        protected override void OnDeactive()
        {
            // 清空场景选择界面上的按钮,因为按钮上关联了场景Metadata
            _viewSelectScene.Clear();
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(_viewSelectScene);
            CoreEventBus.Unregister(_viewSelectScene);

            this.zoomView.Close();

            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            sceneContent.Child = null;
            // 从事件总线撤销注册
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(renderView);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(header);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(sceneContent);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(operationNavigation);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(jsControl);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(zoomView);
            CPPEI.Coolzon.Common.EventBus.Instance.Unregister(this);

            NCLEventBus.Unregister(renderView);
            NCLEventBus.Unregister(header);
            NCLEventBus.Unregister(sceneContent);
            NCLEventBus.Unregister(operationNavigation);
            NCLEventBus.Unregister(jsControl);
            NCLEventBus.Unregister(zoomView);
            NCLEventBus.Unregister(this);

            NCLEventBus.Unregister(renderView);
            UIEventBus.Unregister(header);
            UIEventBus.Unregister(sceneContent);
            UIEventBus.Unregister(operationNavigation);
            UIEventBus.Unregister(jsControl);
            UIEventBus.Unregister(zoomView);
            UIEventBus.Unregister(this);

            CoreEventBus.Unregister(header);
            CoreEventBus.Unregister(sceneContent);
            CoreEventBus.Unregister(operationNavigation);
            CoreEventBus.Unregister(jsControl);
            CoreEventBus.Unregister(zoomView);
            CoreEventBus.Unregister(this);

            CPPEI.Coolzon.Common.EventBus.Create(CPPEI.Coolzon.ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(renderView);

            base.OnDeactive();
        }

        private void closeImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            CPPEI.Coolzon.Common.EventBus.Instance.Publish(new CloseNavEvent());
        }

        private void OnBtnCloseMagnifierClick(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIShowMagnifierEvent(false));
        }
        private void OnBtnCloseLaserPointClick(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIShowLaserPointEvent(false));
        }

        /// <summary>
        /// 打开第一个场景
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnGoToPrevScene_Click(object sender, RoutedEventArgs e)
        {
            if (UIContext.Current.OpenedSceneList != null && UIContext.Current.OpenedSceneList.Count >= 2)
            {
                //先取出当前场景，再取出上一场景
                UIContext.Current.OpenedSceneList.Pop();
                string preSceneName = UIContext.Current.OpenedSceneList.Pop();
                Scene preScene = SolutionUtil.GetScene(UIContext.Current.Solution, preSceneName);
                NCLEventBus.Publish(new NCLOpenSceneEvent(SwitchMode.Replace, "", preScene.Name, null));
            }
        }

        /// <summary>
        /// 打开下一个场景
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctlBtnGoToNextScene_Click(object sender, RoutedEventArgs e)
        {
            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UIContext.Current.RenderView;
            ScenePart sceneInst = renderView.GetCurrentPart() as ScenePart;
            if (sceneInst != null)
            {
                Scene scene = (sceneInst as ScenePart).SceneMatadata;
                Scene nextScene = SolutionUtil.GetNextDisplayflowNode(UIContext.Current.Solution, scene);
                if (nextScene == null)
                {
                    return;
                }
                OpenSceneArgs openSceneArgs = new OpenSceneArgs();
                openSceneArgs.ParametersString.Add("sys:isresetviewstate", "true");
                NCLEventBus.PublishAsync(
                    new NCLOpenSceneEvent(SwitchMode.Replace, "", nextScene.Name, openSceneArgs));
            }
        }

        #region tool button
        private bool _isShowLaser = false;
        private bool _isShowMagnifier = false;
        private void ctlBtnLaser_Click(object sender, RoutedEventArgs e)
        {
            if (_isShowLaser)
            {
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }
            else
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
                UIEventBus.Publish(new UIShowLaserPointEvent(true));
            }
        }

        private void ctlBtnMagnifier_Click(object sender, RoutedEventArgs e)
        {
            if (_isShowMagnifier)
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(false));
            }
            else
            {
                UIEventBus.Publish(new UIShowMagnifierEvent(true));
                UIEventBus.Publish(new UIShowLaserPointEvent(false));
            }
        }

        private void ctlBtnResetSolution_Click(object sender, RoutedEventArgs e)
        {
            CPPEI.Coolzon.Common.EventBus.Instance.Publish(new CPPEI.Coolzon.UI.Views.Events.BeginResetSolutionEvent(UIContext.Current.SolutionName));
        }
        #endregion

        #region Core Event
        [CPPEI.Coolzon.Common.Subscribe]
        public void OnOpened(CoreOpenSceneCompletedEvent @event)
        {
            _scene = SolutionUtil.GetScene(UIContext.Current.Solution, @event.SceneName);
            ctlTextCurScene.Text = @event.SceneName;
        }
        #endregion

        #region UI Event
        MainViewControl.SystemFunction _viewSysFunction = null;
        /// <summary>
        /// 按钮变更事件
        /// </summary>
        /// <param name="event"></param>
        [CPPEI.Coolzon.Common.Subscribe]
        public void NavChange(FastionNavChangeEvent @event)
        {
            if (@event.IsEnter)
            {
                if (string.Equals(@event.Name, Constant.NavEventNames.ShowSelectScenePanel))
                {
                    ctlPopupContainer.Children.Clear();
                    ctlPopupContainer.Children.Add(this._viewSelectScene);
                    _viewSelectScene.Active();
                    ctlPopupContainer.Visibility = System.Windows.Visibility.Visible;
                    ctlPopupBackground.Visibility = System.Windows.Visibility.Visible;
                }
                else if (string.Equals(@event.Name, Constant.NavEventNames.ShowSysFunctionPanel))
                {
                    ctlPopupContainer.Children.Clear();
                    if (this._viewSysFunction == null)
                    {
                        this._viewSysFunction = new SystemFunction();
                    }
                    ctlPopupContainer.Children.Add(this._viewSysFunction);
                    _viewSysFunction.Active();
                    ctlPopupContainer.Visibility = System.Windows.Visibility.Visible;
                    ctlPopupBackground.Visibility = System.Windows.Visibility.Visible;
                }
            }
            else
            {
                if (string.Equals(@event.Name, Constant.NavEventNames.ShowSelectScenePanel))
                {
                    ctlPopupContainer.Children.Remove(this._viewSelectScene);
                    if (ctlPopupContainer.Children.Count == 0)
                    {
                        ctlPopupContainer.Visibility = System.Windows.Visibility.Hidden;
                        ctlPopupBackground.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
                else if (string.Equals(@event.Name, Constant.NavEventNames.ShowSysFunctionPanel))
                {
                    ctlPopupContainer.Children.Remove(this._viewSysFunction);
                    if (ctlPopupContainer.Children.Count == 0)
                    {
                        ctlPopupContainer.Visibility = System.Windows.Visibility.Hidden;
                        ctlPopupBackground.Visibility = System.Windows.Visibility.Hidden;
                    }
                }
            }

            return;
            contentBg.Visibility = Visibility.Visible;

            UIElement viewElement = contentBg.FindName("view") as UIElement;
            if (viewElement != null)
            {
                contentBg.Children.Remove(viewElement);
                contentBg.UnregisterName("view");
            }

            UserControl userControl = null;
            switch (@event.NavName)
            {
                case "Scene selection":
                    userControl = _viewSelectScene;
                    _viewSelectScene.BindScene();
                    break;
                case "System function":
                    userControl = new MainViewControl.SystemFunction();
                    break;
                case "Free interaction":
                    jsControl.Visibility = Visibility.Visible;
                    contentBg.Visibility = Visibility.Hidden;
                    break;
                case "Lecture notes":
                    userControl = new SpeechComment();
                    break;
            }
            if (userControl != null)
            {
                userControl.HorizontalAlignment = HorizontalAlignment.Center;
                userControl.VerticalAlignment = VerticalAlignment.Top;
                userControl.Margin = new Thickness(0, 41, 0, 0);
                contentBg.RegisterName("view", userControl);
                if (!contentBg.Children.Contains(userControl))
                {
                    contentBg.Children.Add(userControl);
                }
            }
        }
        /// <summary>
        /// 关闭中间内容事件
        /// </summary>
        /// <param name="event"></param>
        [CPPEI.Coolzon.Common.Subscribe]
        public void OnCloseCenterContent(CloseNavEvent @event)
        {
            contentBg.Visibility = Visibility.Hidden;
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveUIShowMagnifierEvent(UIShowMagnifierEvent @event)
        {
            if (@event.IsEnable)
            {
                _ctlMagnifierAlert.Visibility = System.Windows.Visibility.Visible;
                _isShowMagnifier = true;
            }
            else
            {
                _ctlMagnifierAlert.Visibility = System.Windows.Visibility.Hidden;
                _isShowMagnifier = false;
            }
        }

        [CPPEI.Coolzon.Common.Subscribe]
        public void OnReceiveUIShowLaserPointEvent(UIShowLaserPointEvent @event)
        {
            if (@event.IsEnable)
            {
                _ctlLaserPointAlert.Visibility = System.Windows.Visibility.Visible;
                _isShowLaser = true;
            }
            else
            {
                _ctlLaserPointAlert.Visibility = System.Windows.Visibility.Hidden;
                _isShowLaser = false;
            }
        }
        #endregion
    }
}
